package servlet;

import bean.Fruit;
import dao.impI.FruitDaoImpI;
import service.ImpI.FruitServiceImpI;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/updateProductServlet",loadOnStartup = 1,
    initParams = {
        @WebInitParam(name="encoding",value = "utf-8")
    }
)
public class UpdateProductServlet extends HttpServlet {
    String encoding=null;
    @Override
    public void init(ServletConfig config) throws ServletException {
        encoding = config.getInitParameter("encoding");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理post请求的编码
        req.setCharacterEncoding(encoding);
        //获取请求数据存入数据库中
        Fruit fruit = new Fruit();
        fruit.setId(Integer.parseInt(req.getParameter("id")));
        fruit.setFname(req.getParameter("fname"));
        fruit.setFprice(Double.parseDouble(req.getParameter("fprice")));
        fruit.setDate(req.getParameter("fdate"));
        fruit.setFweight(Double.parseDouble(req.getParameter("fweight")));
        System.out.println("要更新的数据"+fruit);
        //FruitDaoImpI fruitDaoImpI = new FruitDaoImpI();
        //boolean rs = fruitDaoImpI.updateProduct(fruit);
        FruitServiceImpI fruitServiceImpI = new FruitServiceImpI();
        boolean rs = fruitServiceImpI.updateProduct(fruit);
        if (rs){
            //插入数据成功，转发到这个servlet去查询数据库所有数据然后显示在list.jsp中，只要更改数据都要
            req.getRequestDispatcher("/productListServlet").forward(req,resp);
        }else{
            req.getRequestDispatcher("/exception.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }




}
